package io.jee.runshan.handler;

import io.jee.runshan.domain.Information;
import io.jee.runshan.domain.remote.weixin.Article;
import io.jee.runshan.domain.remote.weixin.InputMessage;
import io.jee.runshan.domain.remote.weixin.OutputImageTextMessage;
import io.jee.runshan.domain.remote.weixin.OutputTextMessage;
import io.jee.runshan.service.IInformationService;
import io.jee.runshan.utils.SpringHolder;
import io.jee.runshan.utils.WeixinConstant;

import java.util.ArrayList;
import java.util.List;

/**
 * 楼层信息处理
 * 
 * @author qinling.zhu
 *
 */
public class WeixinReceiverFloorHandler {

	private IInformationService informationService = SpringHolder.getBean(IInformationService.class);

	public String excute(InputMessage inputMsg) throws Exception {
		String result = "";
		List<Article> list = new ArrayList<Article>();
		List<Information> floorList = informationService.list("type", Information.TYPE_FLOOR, "ASC", "orderIndex");
		OutputImageTextMessage outputMsg = new OutputImageTextMessage();
		Article article = null;
		Information information = null;
		for (int i = 0; i < floorList.size() && i < 10; i++) {
			article = new Article();
			information = floorList.get(i);
			article.setDescription("");
			article.setTitle(information.getTitle());
			article.setPicUrl(WeixinConstant.getRootUrl() + information.getImage());
			article.setUrl(WeixinConstant.getRootUrl() + "/web/information/detail?id=" + information.getId());
			list.add(article);
		}
		if (list.size() > 0) {
			outputMsg.setArticleCount(list.size());
			outputMsg.setArticles(list);
			result = WeixinConstant.genMessage(outputMsg, inputMsg);
		} else {
			OutputTextMessage textMsg = new OutputTextMessage();
			textMsg.setContent("暂无楼层介绍");
			result = WeixinConstant.genMessage(textMsg, inputMsg);
		}
		return result;
	}

}
